package com.vcc.service.impl;

import cn.hutool.core.date.DateUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.Role;
import com.vcc.data.entity.TransferOrder;
import com.vcc.data.mapper.TransferOrderMapper;
import com.vcc.service.boss.response.wellat.TransferOrderVo;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.TransferOrderService;
import org.springframework.stereotype.Service;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.TransferOrderTableDef.TRANSFER_ORDER;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class TransferOrderServiceImpl extends ServiceImpl<TransferOrderMapper, TransferOrder> implements TransferOrderService {
    @Override
    public Page<TransferOrder> paging(
            Integer pageNumber,
            Integer pageSize,
            Role role,
            String orderId,
            String type,
            Long userId,
            Long targetUserId,
            String startDate,
            String endDate) {
        QueryChain<TransferOrder> queryChain = queryChain();
        if (role.getHasAllData()) {
            queryChain.leftJoin(USER).on(USER.ID.eq(TRANSFER_ORDER.USER_ID)).where(
                    USER.PARENT_ID.eq(userId).or(USER.ID.eq(userId))
            );
            if (RegexUtil.checkObjectIsNotNull(targetUserId)) {
                queryChain.where(TRANSFER_ORDER.USER_ID.eq(targetUserId));
            }
        } else {
            queryChain.where(TRANSFER_ORDER.USER_ID.eq(userId));
        }
        if (RegexUtil.checkObjectIsNotNull(startDate)) {
            queryChain.ge(TransferOrder::getCreateDate, DateUtil.beginOfDay(DateUtil.parse(startDate, "yyyy-MM-dd")));
        }
        if (RegexUtil.checkObjectIsNotNull(endDate)) {
            queryChain.le(TransferOrder::getCreateDate, DateUtil.endOfDay(DateUtil.parse(endDate, "yyyy-MM-dd")));
        }
        if (RegexUtil.checkObjectIsNotNull(orderId)) {
            queryChain.eq(TransferOrder::getOrderId, orderId);
        }
        if (RegexUtil.checkObjectIsNotNull(type)) {
            queryChain.like(TransferOrder::getType, type);
        }
        queryChain.orderBy(TransferOrder::getCreateDate, false);
        return queryChain.page(Page.of(pageNumber == null ? 1 : pageNumber, pageSize == null ? 20 : pageSize));
    }

    @Override
    public Page<TransferOrderVo> paging(Integer pageNumber, Integer pageSize, String orderId, String type, String startDate, String endDate, String status, Long userId, Long dealerId) {
        QueryChain<TransferOrder> queryChain = queryChain();
        queryChain.leftJoin(USER).on(USER.ID.eq(TRANSFER_ORDER.USER_ID))
                .select(TRANSFER_ORDER.ALL_COLUMNS)
                .select(USER.ACCOUNT.as("account"));
        if (!BossUserUtil.hasAllData()) {
            Long bossUserId = BossUserUtil.getIdNotNull();
            queryChain.leftJoin(BOSS_USER.as("dealer")).on(USER.BE_LONG_TO_DEALER.eq(BOSS_USER.ID));
            queryChain.and(
                    BOSS_USER.as("dealer").ID.eq(bossUserId)
                            .or(BOSS_USER.as("dealer").PARENT_ID.eq(bossUserId)));
        } else {
            if (RegexUtil.checkObjectIsNotNull(dealerId)) {
                queryChain.leftJoin(BOSS_USER.as("dealer")).on(USER.BE_LONG_TO_DEALER.eq(BOSS_USER.ID));
                queryChain.and(
                        BOSS_USER.as("dealer").ID.eq(dealerId)
                                .or(BOSS_USER.as("dealer").PARENT_ID.eq(dealerId)));
            }
        }
        if (RegexUtil.checkObjectIsNotNull(orderId)) {
            queryChain.eq(TransferOrder::getOrderId, orderId);
        }
        if (RegexUtil.checkObjectIsNotNull(type)) {
            queryChain.like(TransferOrder::getType, type);
        }
        if (RegexUtil.checkObjectIsNotNull(startDate)) {
            queryChain.ge(TransferOrder::getCreateDate, startDate);
        }
        if (RegexUtil.checkObjectIsNotNull(endDate)) {
            queryChain.le(TransferOrder::getCreateDate, endDate);
        }
        if (RegexUtil.checkObjectIsNotNull(status)) {
            queryChain.eq(TransferOrder::getStatus, status);
        }
        if (RegexUtil.checkObjectIsNotNull(userId)) {
            queryChain.eq(TransferOrder::getUserId, userId);
        }
        queryChain.orderBy(TransferOrder::getCreateDate, false);
        return pageAs(
                Page.of(pageNumber == null ? 1 : pageNumber, pageSize == null ? 20 : pageSize),
                queryChain,
                TransferOrderVo.class);
    }
}
